﻿using System.ComponentModel.DataAnnotations;
using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.Sheep;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Common.Infrastructure.Repository;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class TransferSeachHandler : IRequestHandler<TransferSeachCommon, Result>
    {
        IBaseRepository<Sheepvariety> SheepvarietyRepository;
        IBaseRepository<Building> BuildingRepository;
        IBaseRepository<Columns> ColumnsRepository;
        IBaseRepository<Transfer> TransferRepository;
        IBaseRepository<BuildingType> BuildingTypeRepository;
        IBaseRepository<BasicInformation> BasicInformationRepository;
        public TransferSeachHandler(IBaseRepository<Sheepvariety> sheepvarietyRepository,
        IBaseRepository<Building> buildingRepository,
        IBaseRepository<Columns> columnsRepository,
        IBaseRepository<Transfer> transferRepository,
        IBaseRepository<BuildingType> buildingTypeRepository, IBaseRepository<BasicInformation> basicInformationRepository)
        {
            BuildingRepository = buildingRepository;
            ColumnsRepository = columnsRepository;
            TransferRepository = transferRepository;
            BuildingTypeRepository = buildingTypeRepository;
            SheepvarietyRepository=sheepvarietyRepository;
            BasicInformationRepository = basicInformationRepository;
        }

        public async Task<Result> Handle(TransferSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                res.Code = (int)CommonEnumCode.success;
                var list = (from a in TransferRepository.GetAll()
                            join b in BuildingRepository.GetAll()
                            on a.BuildingsEnter equals b.BuildingId
                            join c in BuildingTypeRepository.GetAll()
                            on b.BuildingTypeId equals c.BuildingTypeId
                            join d in ColumnsRepository.GetAll()
                            on a.ColumnsEnter equals d.ColumnsId
                            join e in SheepvarietyRepository.GetAll()
                            on a.SheepvarietyId equals e.SheepvarietyId
                            join f in BasicInformationRepository.GetAll()
                            on a.BreedingSheepArchiveId equals f.BreedingSheepArchiveId
                            select new TransferDTO
                            {
                                TransferId=a.TransferId,
                                TransferDate = DateTime.Now,
                                BreedingSheepArchiveId = a.BreedingSheepArchiveId,
                                Auricle = f.Auricle,
                                BuildingTypeId =b.BuildingTypeId,
                                BuildingTypeName = c.BuildingTypeName,
                                BuildingsOut=b.BuildingId,
                                BuildingName = b.BuildingName,
                                ColumnsOut=d.ColumnsId,
                                ColumnsName=d.ColumnsName,
                                BuildingsEnter =b.BuildingId,
                                BEnter = BuildingRepository.GetAll().Where(x => x.BuildingId == a.BuildingsOut).FirstOrDefault().BuildingName,
                                ColumnsEnter=d.ColumnsId,
                                CEnter = ColumnsRepository.GetAll().Where(x => x.ColumnsId == a.ColumnsOut).FirstOrDefault().ColumnsName,
                                SheepvarietyName = e.SheepvarietyName,
                                TransferSex =a.TransferSex,
                            }).ToList();
                if (!string.IsNullOrEmpty(request.TransferDate))
                {
                    list = list.Where(x => x.TransferDate >= DateTime.Parse(request.TransferDate)).ToList();
                }
                if (!string.IsNullOrEmpty(request.JTransferDate))
                {
                    list = list.Where(x => x.TransferDate <= DateTime.Parse(request.JTransferDate)).ToList();
                }

                if (request.BreedingSheepArchiveId>0)
                {
                    list = list.Where(x => x.BreedingSheepArchiveId==request.BreedingSheepArchiveId).ToList();
                }

                if (request.BuildingTypeId > 0)
                {
                    list=list.Where(x=>x.BuildingTypeId== request.BuildingTypeId).ToList();
                }

                if (request.BuildingsOut > 0)
                {
                    list = list.Where(x => x.BuildingsOut == request.BuildingsOut).ToList();
                }
                if (request.ColumnsOut > 0)
                {
                    list = list.Where(x => x.ColumnsOut == request.ColumnsOut).ToList();
                }
                if (request.BuildingsEnter > 0)
                {
                    list = list.Where(x => x.BuildingsEnter == request.BuildingsEnter).ToList();
                }
                if (request.ColumnsEnter > 0)
                {
                    list = list.Where(x => x.ColumnsEnter == request.ColumnsEnter).ToList();
                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "羊场转舍显示";
            }
            catch (Exception ex)
            {

                throw;
            }
            return res;
        }
    }
    public class TransferDTO
    {
        /// <summary>
        /// 转舍主键
        /// </summary>
        [Key]
        public int TransferId { get; set; }

        /// <summary>
        /// 羊只耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 羊只耳号名称
        /// </summary>
        public string Auricle {  get; set; }
        /// <summary>
        /// 转舍日期
        /// </summary>
        public DateTime TransferDate {  get; set; }

        /// <summary>
        /// 转出栋舍id
        /// </summary>
        public int BuildingsOut { get; set; }
        /// <summary>
        /// 转出栋舍名称
        /// </summary>
        public string BuildingName { get; set; }
        /// <summary>
        /// 转出栏位id
        /// </summary>
        public int ColumnsOut { get; set; }
        /// <summary>
        /// 转出栏位名称
        /// </summary>
        public string? ColumnsName { get; set; }
        /// <summary>
        /// 转入栋舍id
        /// </summary>
        public int BuildingsEnter { get; set; }
        /// <summary>
        /// 转入栋舍名称
        /// </summary>
        public string BEnter { get; set; }
        /// <summary>
        /// 栋舍类型名称
        /// </summary>
        public string? BuildingTypeName { get; set; }
        /// <summary>
        /// 栋舍类型id
        /// </summary>
        public int BuildingTypeId { get; set; }
        /// <summary>
        /// 转入栏位id
        /// </summary>
        public int ColumnsEnter { get; set; }
        /// <summary>
        /// 转入栏位名称
        /// </summary>
        public string CEnter {  get; set; }
        /// <summary>
        /// 品种名称
        /// </summary>
        public string? SheepvarietyName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public int TransferSex { get; set; }
    }
}
